/**   
 * @Title: Table.java 
 * @Package org.playgod1984.du.ddesign.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月27日 下午3:23:33 
 * @version V1.0   
 */
package org.playgod1984.du.ddesign.model;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.playgod1984.du.base.util.DuDesignUtil;

import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @ClassName: Table 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月27日 下午3:23:33 
 *  
 */
@SuppressWarnings("all")
public class Table extends Model<Table> {
	private static final Logger log = Log4jLogger.getLogger(Table.class);
	public static final Table dao = new Table();
	/**
	 * @Title: getTablesWithPagenate
	 * @author:playgod playgod1984@163.com 
	 * @Description: 分页查找表
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<Table>    返回类型 
	 * @throws
	 */
	public Page<Table> getTablesWithPagenate(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sql_where=" left join dud_auth_user u on fie.creator = u.loginaccount where fie.diagramid='" + params.get("diagramid") + "'";
		if(!StringUtils.isBlank((String)params.get("bm"))){
			sql_where+=" and (fie.tablepname like '%"+params.get("bm") +"%' or fie.tablelname like '%"+params.get("bm")+ "%')";
		}
		String sqlselect="SELECT fie.tableid,fie.tablepname,fie.tablelname,u.displayname,u.createtime ";
		return paginate(pageNumber, pageSize, sqlselect, "FROM dud_design_table fie" + sql_where);
	}
	/**
	 * @Title: findPD
	 * @author:playgod playgod1984@163.com 
	 * @Description: 寻找PD
	 * @param @param diagramid
	 * @param @return    设定文件 
	 * @return Record    返回类型 
	 * @throws
	 */
	public Record findPD(String diagramid){
		String sql="SELECT d.diagramid,d.diagramname,p.projectname,p.projectcode FROM dud_design_diagram d"+
					" INNER JOIN dud_ddesign_pro p"+
					" ON d.projectid = p.projectcode"+
					" WHERE d.diagramid=?";
		return Db.findFirst(sql,diagramid);
	}
	/**
	 * @Title: findByPName
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Table    返回类型 
	 * @throws
	 */
	public Table findByPName(Map params){		
		String sql="select tablelname from "
				+ "	dud_design_table where tablepname='"+params.get("tablepname")+"'"
						+ " AND projectid='"+ params.get("projectid") +"'";
		return findFirst(sql);
	}
	/**
	 * @Title: findByName
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取同名图表 
	 * @param @param diagramname
	 * @param @return    设定文件 
	 * @return Diagram    返回类型 
	 * @throws
	 */
	public Table findByLName(Map params){		
		String sql="select tablepname from "
				+ "	dud_design_table where tablelname='"+params.get("tablelname")+"'"
						+ " AND projectid='"+ params.get("projectid") +"'";
		return findFirst(sql);
	}
	
	/**
	 * @Title: save
	 * @author:playgod playgod1984@163.com 
	 * @Description: 保存
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public String save(Map params){
		Record r = new Record();
		r.set("tablepname", params.get("tablepname"));
		r.set("tablelname", params.get("tablelname"));
		r.set("projectid", params.get("projectid"));
		r.set("diagramid", params.get("diagramid"));
		r.set("remark", params.get("remark"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		try{
			if("A".equals((String)params.get("screen"))){//ADD
				r.set("tableid", DuDesignUtil.getUUID32());
				r.set("creator", params.get("currentUser"));
				r.set("createtime",new Date());				
				Db.save("dud_design_table", r);
			}else{
				r.set("tableid", params.get("tableid"));
				Db.update("dud_design_table", "tableid", r);
			}
			return r.getStr("tableid");
		}catch(Exception ex){
			log.error(ex.getMessage());
			return "";
		}
	}
	/**
	 * @Title: deleteRows
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRows(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				if(dao.deleteById(ids[i])){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
	/**
	 * @Title: deleteRelation
	 * @author:playgod playgod1984@163.com 
	 * @Description: 删除指标
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRelation(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				Record r = new Record();
				r.set("relationid", ids[i]);
				if(Db.delete("dud_design_tr","relationid", r)){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
	/**
	 * @Title: getRelationTable
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取关系表ID
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<Record>    返回类型 
	 * @throws
	 */
	public Page<Record> getRelationTable(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sqlselect="SELECT r.leftfield,r.relationid,r.rightfield,r.tableid,"+
				" lf.fieldname lfname,rf.fieldname rfname,rt.tablelname rtablelname,rt.tablepname rtablepname "+
				" ,t.tablelname,t.tablepname";
		String sql_where = 
				" FROM dud_design_tr  r"+
				" INNER JOIN dud_design_field lf"+
				" ON r.leftfield  = lf.fieldid"+
				" INNER JOIN dud_design_field rf"+
				" ON r.rightfield = rf.fieldid"+
				" INNER JOIN dud_design_table t"+
				" ON r.tableid = t.tableid"+
				" INNER JOIN dud_design_table rt"+
				" ON r.rtableid = rt.tableid"+
				" WHERE r.tableid='"+params.get("tableid")+"'";
		return Db.paginate(pageNumber, pageSize, sqlselect, sql_where);
	}
	/**
	 * @Title: getFieldList
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取字段ID
	 * @param @param params
	 * @param @return    设定文件 
	 * @return List<Record>    返回类型 
	 * @throws
	 */
	public List<Record> getFieldList(Map params){
		String sql="SELECT f.fieldid,f.fieldname  FROM dud_design_field f where f.tableid='"+params.get("tableid")+"'";
		return Db.find(sql);
	}
	/**
	 * @Title: getTablesList
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取所有表
	 * @param @param params
	 * @param @return    设定文件 
	 * @return List<Record>    返回类型 
	 * @throws
	 */
	public List<Record> getTablesList(Map params){
		String sql = "SELECT t.tableid,t.tablelname FROM dud_design_table t ";
				//+ "WHERE t.tableid !='"+params.get("tableid")+"'";
		return Db.find(sql);
	}
	/**
	 * @Title: saveRelation
	 * @author:playgod playgod1984@163.com 
	 * @Description: 保存表关系
	 * @param @param params
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public String saveRelation(Map params){
		Record r = new Record();
		r.set("tableid", params.get("tableid"));
		r.set("leftfield", params.get("lfield"));
		r.set("rightfield", params.get("rfield"));
		r.set("rtableid", params.get("rtable"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		try{
			if("A".equals((String)params.get("screen"))){//ADD
				r.set("relationid", DuDesignUtil.getUUID32());
				r.set("creator", params.get("currentUser"));
				r.set("createtime",new Date());				
				Db.save("dud_design_tr", r);
			}
			return r.getStr("relationid");
		}catch(Exception ex){
			log.error(ex.getMessage());
			return "";
		}
	}
}
